package xulong.com.weiyulinked.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xulong.com.weiyulinked.model.Result;
import xulong.com.weiyulinked.model.SessionUser;
import xulong.com.weiyulinked.model.UserInfo;
import xulong.com.weiyulinked.model.message;
import xulong.com.weiyulinked.service.MessageSessionService;
import xulong.com.weiyulinked.service.UserService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MessageSessionService messageSessionService;

    @RequestMapping("/login")
    public Result<UserInfo> userLogin(@RequestBody UserInfo userInfo, HttpSession session) {
        log.info("用户登录：{}", userInfo);
        // 校验用户名和密码是否为空
        if (!StringUtils.hasLength(userInfo.getUsername()) || !StringUtils.hasLength(userInfo.getPassword())) {
            return Result.fail("用户名或密码不能为空");
        }
        // 调用服务层登录方法
        Result<UserInfo> result = userService.userLogin(userInfo,session);
        return result;
    }

    @RequestMapping("/register")
    public Result<UserInfo> userRegister(@RequestBody UserInfo userInfo) {
        log.info("用户注册：{}", userInfo);
        // 校验用户名和密码是否为空
        if (!StringUtils.hasLength(userInfo.getUsername()) || !StringUtils.hasLength(userInfo.getPassword())) {
            return Result.fail("用户名或密码不能为空");
        }
        // 调用服务层注册方法
        return userService.userRegister(userInfo);
    }


    @RequestMapping("/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request){
        log.info("获取用户信息");
        //获取用户信息，放在client界面
        HttpSession session = request.getSession(false);
        if (session==null) {
            return Result.fail("用户未登录");
        }
        UserInfo user = (UserInfo)session.getAttribute("user");
        if (user==null) {
            return Result.fail("用户未登录");
        }
        log.info("用户信息：{}", user);
        return Result.success(user);
    }

    @RequestMapping("/getFriendList")
    public Result<List<UserInfo>> getFriendList(HttpServletRequest request){
        log.info("获取好友列表");
        //获取用户信息，放在client界面
        HttpSession session = request.getSession(false);
        if (session==null) {
            return Result.fail("用户未登录");
        }
        UserInfo user = (UserInfo)session.getAttribute("user");
        if (user==null) {
            return Result.fail("用户未登录");
        }
        // 调用服务层获取好友列表方法
        log.info("用户id：{}", user.getId());
        List<UserInfo> friendList = userService.getFriendList(user.getId());
        log.info("好友列表：{}", friendList);
        return Result.success(friendList);
    }

    @RequestMapping("/getSessionList")
    public Result<List<SessionUser>> getSessionList(HttpServletRequest request){
        //TODO 从session中获取会话信息
        HttpSession session = request.getSession(false);
        if (session==null) {
            return Result.fail("用户未登录");
        }
        //获取当前登录用户
        UserInfo user = (UserInfo)session.getAttribute("user");
        if (user==null) {
            return Result.fail("用户未登录");
        }
        //获取当前登录用户ID
        Integer userId = user.getId();
        // 调用服务层获取会话列表方法
        List<SessionUser> sessionList = messageSessionService.getSessionList(userId);
        log.info("会话列表：{}", sessionList);
        return Result.success(sessionList);
    }

    //点击好友项时创建新会话
    @RequestMapping("/createSession")
    public Result<Integer> createSession( Integer friendId, HttpServletRequest request){
        log.info("创建会话：{}", friendId);
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        Object attribute = session.getAttribute("user");
        if (attribute==null) {
            return Result.fail("用户未登录");
        }
        UserInfo user = (UserInfo) attribute;
        // 调用服务层创建会话方法
        Integer sessionId = messageSessionService.createSession(user.getId(), friendId);
        return Result.success(sessionId);
    }

    //检查会话是否存在
    @RequestMapping("/checkSessionExists")
    public Result<SessionUser> checkSessionExists( Integer friendId, HttpServletRequest request){
        log.info("检查会话是否存在：{}", friendId);
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        Object attribute = session.getAttribute("user");
        if (attribute==null) {
            return Result.fail("用户未登录");
        }
        UserInfo user = (UserInfo) attribute;
        //检查会话是否存在
        SessionUser sessionUser = messageSessionService.checkSessionExists(user.getId(), friendId);
        return Result.success(sessionUser);
    }

    //获取会话中的历史消息
    @RequestMapping("/getMessage")
    public Result<List<message>> getMessage( Integer sessionId){
        log.info("获取会话中的历史消息：{}", sessionId);
        // 调用服务层获取历史消息方法
        List<message> messageList = messageSessionService.getMessage(sessionId);
        return Result.success(messageList);
    }

}
